package edu.ruc.cluster;

import javax.swing.JFrame;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import edu.ruc.cluster.test.TimeDialog;
import edu.ruc.cluster.ui.MachineListPane;
import edu.ruc.cluster.ui.HadoopUserLoginPane;
import edu.ruc.cluster.ui.JdkPane;
import edu.ruc.cluster.ui.UserPane;
import edu.ruc.cluster.ui.HadoopFilePane;
import edu.ruc.cluster.action.Entrance;

@SuppressWarnings("serial")
public class ClusterConfiguration extends JFrame {
	
	private JPanel textPane = null;
	private JPanel buttonPanel = null;
	private CardLayout card = null;
	private JButton previous = null;
	private JButton next = null;
	private JButton cancel = null;
	private List<JPanel> panels;
	private int currentCard = 0;
	
	private MachineListPane listp = new MachineListPane();
	private UserPane userp = new UserPane();
	private HadoopUserLoginPane loginp = new HadoopUserLoginPane();
	private JdkPane jdkp = new JdkPane();
	private HadoopFilePane hfilep = new HadoopFilePane();
	
	public ClusterConfiguration(){
		super("hadoop cluster configuration");
		setPanel();
//		boolean finish = false;
//		boolean back = false;
//		while(true){
//			finish = setPanel();
//			if(finish == true){
//				back = changePanel();
//				if(back == true)
//					continue;
//				else
//					break;
//			}
//		}
	}
	public void setPanel(){
	
		try{
//			UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
//			UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
//			MetalLookAndFeel.setCurrentTheme(new OceanTheme());
//			UIManager.setLookAndFeel(new MetalLookAndFeel());
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		setBounds(100, 100, 320, 315);
		card = new CardLayout(30, 30);
		textPane = new JPanel(card);
		buttonPanel = new JPanel();
		
		previous = new JButton("<Back");
		next = new JButton("Next>");
		cancel = new JButton("Cancel");
		
		buttonPanel.add(previous);
		buttonPanel.add(next);
		buttonPanel.add(cancel);
		
		
		panels = new ArrayList<JPanel>();
		 //add different panels here
		panels.add(listp);
		panels.add(userp);
		panels.add(loginp);
		panels.add(jdkp);
		panels.add(hfilep);
		
		Iterator<JPanel> iter = panels.iterator();
		while(iter.hasNext()){
			textPane.add(iter.next(), "name");
		}
		
		previous.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				
				
				if(currentCard == panels.size()){
					next.setText("Next>");
				}
//					System.out.println("currentNumber : " + currentCard + "panels size: " + panels.size());

				card.previous(textPane);			
				currentCard --;
				if(currentCard == 1){
					previous.setEnabled(false);
				}
			}
		});
		
		next.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(next.getText().equals("Finish")){
					
						// TODO: make sure how to print some message on this TimeDialog
					TimeDialog timer = new TimeDialog();
					timer.showDialog(new JFrame(), "Please wait for configuration......", 5);
//					JOptionPane.showMessageDialog(null, "System is configurating...",
//							"Message", JOptionPane.INFORMATION_MESSAGE);
//						// new added:
					Entrance entrance = new Entrance();
					entrance.Run();
					
				}
				else{
					switch(currentCard){
					case 0:
						//TODO: call checkInput						
						listp.checkInput();
						if(listp.nextReady()){
							listp.store();
							userp.removeAll();
							userp.constructPane();
							card.next(textPane);
							currentCard ++;
						}else{
							JOptionPane.showMessageDialog(listp, 
									"there's incorrect format for your input!", "message", 
									JOptionPane.ERROR_MESSAGE);
						}
						break;
					case 1:
						if(userp.ok()){
							userp.store();
							loginp.removeAll();
							loginp.constructPane();
							card.next(textPane);
							currentCard ++;
						}else{
							JOptionPane.showMessageDialog(userp, 
									"there's incorrect format for your input!", "message", 
									JOptionPane.ERROR_MESSAGE);
						}
						break;
					case 2:
						if(loginp.ok() == true){
							loginp.store();
							jdkp.removeAll();
							jdkp.constructPane();
							card.next(textPane);
							currentCard ++;
						}else{
							JOptionPane.showMessageDialog(loginp, 
									"there's incorrect format for your input!", "message", 
									JOptionPane.ERROR_MESSAGE);
						}
						break;
					case 3:
						if(jdkp.ok() == true){
							jdkp.store();
							hfilep.removeAll();
							hfilep.consturctPane();
							card.next(textPane);
							currentCard ++;
						}
						else{
							JOptionPane.showMessageDialog(jdkp, 
									"there's incorrect format for your input!", "message", 
									JOptionPane.ERROR_MESSAGE);
						}
						break;
					case 4:
						if(hfilep.ok()){
							hfilep.store();
							currentCard++;
						}
						else{
							JOptionPane.showMessageDialog(hfilep, 
									"there's incorrect format for your input!", "message", 
									JOptionPane.ERROR_MESSAGE);
						}
						break;
					default:
						break;
					}
					if(previous.isEnabled()==false)
						previous.setEnabled(true);
//					System.out.println("currentNumber : " + currentCard + "panels size: " + panels.size());
					if(currentCard == panels.size()){
						next.setText("Finish");
				/*
				else{
					switch(currentCard){
					case 1:
						//TODO: call checkInput						
						MachineListPane mp = (MachineListPane)textPane.getComponent(currentCard - 1);
						mp.checkInput();
						if(mp.nextReady()){
							mp.store();
							UserPane up = (UserPane)textPane.getComponent(currentCard);
							up.removeAll();
							up.constructPane();
							card.next(textPane);
							currentCard ++;
						}else{
							JOptionPane.showMessageDialog(mp, 
									"there's incorrect format for your input!", "message", 
									JOptionPane.ERROR_MESSAGE);
						}
						break;
					case 2:
						UserPane pp = (UserPane)textPane.getComponent(currentCard - 1);
						if(pp.ok()){
							pp.store();
							HadoopUserLoginPane hp = (HadoopUserLoginPane)textPane.getComponent(currentCard);
							hp.removeAll();
							hp.constructPane();
							card.next(textPane);
							currentCard ++;
						}else{
							JOptionPane.showMessageDialog(pp, 
									"there's incorrect format for your input!", "message", 
									JOptionPane.ERROR_MESSAGE);
						}
						break;
					case 3:
						HadoopUserLoginPane hp = (HadoopUserLoginPane)textPane.getComponent(currentCard - 1);
						if(hp.ok() == true){
							hp.store();
							JdkPane jdkp = (JdkPane)textPane.getComponent(currentCard);
							jdkp.removeAll();
							jdkp.
							card.next(textPane);
							currentCard ++;
						}else{
							JOptionPane.showMessageDialog(hp, 
									"there's incorrect format for your input!", "message", 
									JOptionPane.ERROR_MESSAGE);
						}
					case 4:
						JdkPane jdkp = (JdkPane)textPane.getComponent(currentCard - 1);
						if(jdkp.ok()){
							jdkp.store();
							
							card.next(textPane);
							currentCard ++;
						}
						else{
							JOptionPane.showMessageDialog(jdkp, 
									"there's incorrect format for your input!", "message", 
									JOptionPane.ERROR_MESSAGE);
						}
					case 5:
						HadoopFilePane hadoopp = (HadoopFilePane)textPane.getComponent(currentCard - 1);
						if(hadoopp.ok()){
							hadoopp.store();
						}
						else{
							JOptionPane.showMessageDialog(hadoopp, 
									"there's incorrect format for your input!", "message", 
									JOptionPane.ERROR_MESSAGE);
						}
					default:
						break;
					}
					*/	
					
						
					}
				}
			}
		});
		
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int reply = JOptionPane.showConfirmDialog(null, "Are you really want to quit the configuration?",
						"Confirm", JOptionPane.YES_NO_OPTION);
				if(reply == JOptionPane.YES_OPTION)
				{
					System.exit(0);
				}
			}
		});
		this.getContentPane().add(textPane);
		this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("hadoop cluster configuration");
		this.setSize(800, 600);
		this.setVisible(true);
		
	}
	
	public void changePanel(){
		
		this.getContentPane().remove(buttonPanel);
		previous = new JButton("<Back");		
		String str = next.getText().equals("Finish")? "Next>" : "Finish";
		next = new JButton(str);
		cancel = new JButton("Cancel");
//		next.addActionListener(new ActionListener(){
//			public void actionPerformed(ActionEvent e){
//				JOptionPane.showMessageDialog(null, "System is configurating...",
//						"Message", JOptionPane.INFORMATION_MESSAGE);
//			}
//		});
//		previous.addActionListener(new ActionListener(){
//			public void actionPerformed(ActionEvent e){
//							
//				card.previous(textPane);			
//				currentCard --;
//				setPanel();
//				
//			}
//		});
//		cancel.addActionListener(new ActionListener(){
//			public void actionPerformed(ActionEvent e){
//				int reply = JOptionPane.showConfirmDialog(null, "Are you really want to quit the configuration?",
//						"Confirm", JOptionPane.YES_NO_OPTION);
//				if(reply == JOptionPane.YES_OPTION)
//				{
//					System.exit(0);
//				}
//			}
//		});
		buttonPanel.add(previous);
		buttonPanel.add(next);
		buttonPanel.add(cancel);
		this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
		this.getContentPane().validate();
		this.getContentPane().repaint();
		return;
		
	}
//	class ChangePanel implements Runnable(){
//		ChangePanel(){
//			previous = new JButton("<Back");
//			next = new JButton("Next>");
//			cancel = new JButton("Cancel");
//			
//			buttonPanel.add(previous);
//			buttonPanel.add(next);
//			buttonPanel.add(cancel);
//		}
//		public void run(){
//			(buttonPanel);
//		}
//	}
	public static void main(String[] args){
		new ClusterConfiguration();
	}
}
